package com.IR.order.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.IR.shop.mapper.CarsMapper;
import com.IR.shop.mapper.NormsMapper;
import com.IR.shop.mapper.PreorderMapper;
import com.IR.shop.mapper.ProductMapper;
import com.IR.shop.po.Cars;
import com.IR.shop.po.Norms;
import com.IR.shop.po.Preorder;
import com.IR.shop.po.Product;


@Service
public class APreorderBiz {
	@Resource
	public PreorderMapper mapper;
	@Resource
	public ProductMapper mapper1;
	@Resource
	public NormsMapper mapper2;
	@Resource
	public CarsMapper mapper3;
	@Resource
	public BPreorderBiz biz;
	private Preorder preorder;
	private List<Preorder> list;
	public Preorder getPreorder() {
		return preorder;
	}
	public void setPreorder(Preorder preorder) {
		this.preorder = preorder;
	}
	public List<Preorder> getList() {
		return list;
	}
	public void setList(List<Preorder> list) {
		this.list = list;
	}
	//全部删除
	public void deletePreorder() {
		list=biz.showPreorderAll();
		for(Preorder p:list) {
			mapper.deleteByPrimaryKey(p.getPreorderid());
		}
	}
	//立即购买伪订单
	public void savePreorder1(Long userid,Long proid,int normsid) {
		list=biz.showPreorderAll();
		Preorder pre=new Preorder();
		if(list.size()==0) {		
			pre.setPreorderid(1);
		}
		if(list.size()!=0){
			list=biz.showPreorderby();
			preorder=list.get(0);
			int preorderid=preorder.getPreorderid()+1;
			pre.setPreorderid(preorderid);
		}		
		pre.setUserid(userid);
		pre.setProid(proid);
		Product p=new Product();
		p=mapper1.selectByPrimaryKey(proid);
		pre.setProimgs(p.getProimgs());
		pre.setProname(p.getProname());
		pre.setPronumber(1);
		pre.setProprice(p.getProprice());
		pre.setNormsid(normsid);
		Norms n=new Norms();
		n=mapper2.selectByPrimaryKey(normsid);
		pre.setNormsdetail(n.getNormsdetail());
		mapper.insert(pre);
	}
	//购物车购买伪订单
	public void savePreorder1(Long userid,List<Integer> list) {
		for(int i=0;i<list.size();i++) {
			Cars c=new Cars();
			c=mapper3.selectByPrimaryKey(list.get(i));
			Preorder pre=new Preorder();
			pre.setUserid(userid);
			pre.setProid(c.getProid());
			Product p=new Product();
			p=mapper1.selectByPrimaryKey(c.getProid());
			pre.setProimgs(p.getProimgs());
			pre.setProname(p.getProname());
			pre.setProprice(p.getProprice());
			pre.setPronumber(c.getPronumber());
			pre.setNormsid(c.getNormsid());
			Norms n=new Norms();
			n=mapper2.selectByPrimaryKey(c.getNormsid());
			pre.setNormsdetail(n.getNormsdetail());
			mapper.insert(pre);
		}
	}
}
